#pragma once
#include "StateBase.h"
#include <stack>
#include <vector>
#define GetStateStack()		StateStack::GetInstance()

class StateStack
{
protected:
	StateStack () {}
	~StateStack () 
	{
		while ( !m_stack.empty() )
		{
			StateBase* state = m_stack.top();
			delete state;
			m_stack.pop();
		}

		for (std::vector<StateBase*>::iterator it=m_recycle.begin(); it!=m_recycle.end(); )
		{
			delete (*it);
			it = m_recycle.erase(it);
		}
	}

	/*implicit methods exist for the copy constructor and operator= and we want to forbid calling them.*/
	StateStack (const StateStack &){} 
	StateStack& operator = (const StateStack &) {}

public:
	static void CreateInstance ()
    {
        if ( ms_pInstance == NULL )
		   ms_pInstance = new StateStack();
    }
	static StateStack * GetInstance () 
    {
	   return ms_pInstance;
    }
	static void DestroyInstance () 
    {
	   if ( ms_pInstance )
	   {
		 delete ms_pInstance;
		 ms_pInstance = NULL;
	   }
    }
	
	StateBase*		GetCurrentState ();
	void			SetState (StateBase* state);
	void			PushState (StateBase* state);
	void			PopState ();
	
	
protected:
	static StateStack * ms_pInstance;
	std::stack<StateBase*> m_stack;
	std::vector<StateBase*> m_recycle;
};